import winsound
from pynput.keyboard import Key, Controller
from tkinter import ttk, messagebox, scrolledtext, Radiobutton
from tkinter import *


class BaseOutput:
    def __init__(self):
        self.stop = False
        self.isEnd = True
        self.isPressEnter = True # 是否按下回车

    # 键盘输出文字 content 内容
    def keyResultContent(self, content):
        kayCon = Controller()
        kayCon.type(content)
        if self.isPressEnter:
            kayCon.press(Key.enter)
            kayCon.release(Key.enter)

    # 创建公共配置
    def commonConfig(self, root):
        cBox = ttk.LabelFrame(root, text='公共配置')
        cBox.pack(padx=10, pady=10)
        dev = IntVar()
        dev.set(2)


        row = 0
        def on_radio_button_selected(value):
            self.isPressEnter = value.get() == '1'
        selected_value = StringVar(value='0')
        if self.isPressEnter:
            selected_value.set('1')
            
        ttk.Label(cBox, text='自动按Enter键').grid(row=row, column=0, sticky=W)
        Radiobutton(cBox, text='是', variable=selected_value, value='1', command=lambda: on_radio_button_selected(selected_value)).grid(row=row, column=1, sticky=W)
        Radiobutton(cBox, text='否', variable=selected_value, value='0', command=lambda: on_radio_button_selected(selected_value)).grid(row=row, column=2, sticky=W)
        
        row += 1
        ttk.Label(cBox, text='输出速度(毫秒)').grid(row=row, column=0)
        self.speed = Scale(cBox, from_=50, to=5000, orient=HORIZONTAL)
        self.speed.grid(row=row, column=1, sticky=W+E+N+S)
        self.speed.set(100)

        row += 1
        ttk.Label(cBox, text='测试输出').grid(row=row, column=0)
        scrolledtext.ScrolledText(cBox, height=6).grid(row=row, column=1)

        return cBox

    # 提示
    def tips(self, message):
        messagebox.showinfo(title='提示', message=message)

    def get_count(self):
        return int(self.count.get())

    def get_speed(self):
        return int(self.speed.get()) / 1000

    def get_strtemp(self):
        return self.strtemp.get(1.0, END).strip('\n')

    # 执行输出入口
    def runMain(self):
        print('>>> 开始 >>>')
        self.stop = False
        self.isEnd = False
        self.run()
        self.stop = True
        self.isEnd = True
        print('>>> 结束 >>>')
        winsound.Beep(1000, 50)

    # abstract 创建配置
    def createConfig(self):
        print('config 子类需实现')
        box = ttk.LabelFrame(self.root, text='配置')
        return box

    # abstract 执行输出
    def run(self):
        print('run 子类需实现')
